package service

import (
	"admin-api/app/models/request"
	"admin-api/app/models/response"
	"admin-api/core"
)

var Logger = new(LoggerService)

type LoggerService struct{}

// VisitPage 访问日志分页查询
func (s LoggerService) VisitPage(param *request.VisitLogRequest) (*response.PageData, *response.BusinessError) {
	var (
		list  []response.VisitLogResponse
		total int64
		err   error
	)
	if err = core.DB.Namespace("logger").
		DQuery("userLoginLogPage", param).
		DOffset(param.Page, param.Size).
		TPage(&list, &total).
		Error; err != nil {
		core.Log.Error("查询访问数据失败, 异常信息如下：%s", err.Error())
		return nil, response.CustomBusinessError(response.Failed, "获取访问数据失败")
	}
	return &response.PageData{
		Total: total,
		Page:  param.Page,
		Size:  param.Size,
		Data:  list,
	}, nil
}

// OperatePage 操作日志分页查询
func (s LoggerService) OperatePage(param *request.OperateLogRequest) (*response.PageData, *response.BusinessError) {
	var (
		list  []response.OperateLogResponse
		total int64
		err   error
	)
	if err = core.DB.Namespace("logger").
		DQuery("operateLogQueryPage", param).
		DOffset(param.Page, param.Size).
		TPage(&list, &total).
		Error; err != nil {
		core.Log.Error("统计操作数据失败, 异常信息如下：%s", err.Error())
		return nil, response.CustomBusinessError(response.Failed, "获取操作数据失败")
	}
	return &response.PageData{
		Total: total,
		Page:  param.Page,
		Size:  param.Size,
		Data:  list,
	}, nil
}
